﻿// ===================================
// The use and distribution terms for this software are covered by the Microsoft public license, 
// visit for more info : http://testcommander.codeplex.com 
// 
// You must not remove this copyright notice, or any other, from this software
// 
// Senthil Maruthaiappan  senips@gmail.com
// ===================================

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QAPro.Test.Addin.Fitnesse;
using QAPro.Test.FitCommander;
using QAPro.Test.FitServices.Interfaces;
using QAPro.Test.Interfaces;
using fit;

namespace UnitTests.QAPro.Test.Library
{
    [TestClass]
    public class TestTestEngine
    {
        private readonly TestLocalFitServiceContext _testServiceContext = new TestLocalFitServiceContext();


        [TestMethod]
        public void TestAFitDocument()
        {
            Configuration.Instance.Assemblies.Add("eg.dll");
            var suite = _testServiceContext.Services.Get<ISuite>("Test Suite");
            var fitUtilSvc = new FitUtilService();

            var sampleFixture = new List<string>
                                    {
                                        "!|eg.ArithmeticColumnFixture|",
                                        "|x|y|plus()|",
                                        "|10|2|12|",
                                        "|12|3|15|",
                                        "|100|4|105|"
                                    };


            ITestCase test = suite.CreateTestCase("New Test Case");
            IFixtureTable ftable = test.CreateFixtureTable("Sample Fixture Table-1");
            fitUtilSvc.LoadData(ftable.FixtureData, sampleFixture.ToArray());

            ITestEngine engine = _testServiceContext.Services.Get<IFitTestEngine>();
            test.IsRunnable = true;
            engine.Run(test);
            Assert.AreEqual(2, test.TestResult.Right);
            Assert.AreEqual(1, test.TestResult.Wrong);
        }

        [TestMethod]
        public void TestLocalFitScriptProvider()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection01\FitnesseRoot\GrandSuite1");
            Assert.IsNotNull(scriptProvider, "script provider is null");
            scriptProvider.LoadScripts();
            Assert.AreEqual(1, scriptProvider.RootSuite.Count);
            Assert.AreEqual(3, scriptProvider.RootSuite[0].Count);
            Assert.AreEqual(7, scriptProvider.RootSuite[0][1].Count);

            Assert.IsNotNull(scriptProvider.RootSuite[0][1] as ISuite);
            Assert.IsNotNull(scriptProvider.RootSuite[0][2] as ISuite);
            Assert.IsNotNull(scriptProvider.RootSuite[0][2][2] as ITestCase);

            ITestNode aTestCase = scriptProvider.RootSuite[0][2][1][6];
            Assert.AreEqual(7, aTestCase.Count);
            Assert.AreEqual("TestCase11", aTestCase.Name);

            Assert.IsNotNull(aTestCase[0] as ITestLink);
            Assert.IsNotNull(aTestCase[1] as IFixtureTextData);
            Assert.IsNotNull(aTestCase[2] as IFixtureTable);
            Assert.IsNotNull(aTestCase[3] as IFixtureTextData);
            Assert.IsNotNull(aTestCase[4] as IFixtureTable);
            Assert.IsNotNull(aTestCase[5] as IFixtureTextData);
            Assert.IsNotNull(aTestCase[6] as IFixtureTable);
        }

        [TestMethod]
        public void TestLoadOneTestCase()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection02\FitnesseRoot\GrandSuite1\Suite1\TestCase2");
            Assert.IsNotNull(scriptProvider, "script provider is null");

            scriptProvider.LoadScripts();
            Assert.AreEqual(1, scriptProvider.RootSuite.Count);
            Assert.AreEqual(3, scriptProvider.RootSuite[0].Count);
            Assert.AreEqual(6, scriptProvider.RootSuite[0][1].Count);
            Assert.AreEqual(6, scriptProvider.RootSuite[0][1].Count);
            Assert.AreEqual(0, scriptProvider.RootSuite[0][1][0].Count);
        }

        [TestMethod]
        public void TestLoadOneTestCaseWithIncludeCase1()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection02\FitnesseRoot\GrandSuite1\Suite1\TestCase1");
            Assert.IsNotNull(scriptProvider, "script provider is null");

            scriptProvider.LoadScripts();
            Assert.AreEqual(1, scriptProvider.RootSuite.Count);
            Assert.AreEqual(3, scriptProvider.RootSuite[0].Count);
            Assert.AreEqual(6, scriptProvider.RootSuite[0][1].Count);
            Assert.AreEqual(5, scriptProvider.RootSuite[0][2].Count);
            Assert.AreEqual(7, scriptProvider.RootSuite[0][2][1].Count);
            Assert.AreEqual(7, scriptProvider.RootSuite[0][2][1][6].Count);
            Assert.IsTrue(scriptProvider.RootSuite[0][2][1][6][0] is ITestLink);
            Assert.AreEqual(1, scriptProvider.RootSuite[0][2][1][6][0].Count);
        }

        [TestMethod]
        public void TestGetVariables()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection02\FitnesseRoot\GrandSuite1\Suite1\TestCase2");
            Assert.IsNotNull(scriptProvider, "script provider is null");

            scriptProvider.LoadScripts();

            var utilSvc = _testServiceContext.Services.Get<IFitUtilService>();
            IList<string> list = new List<string>();
            utilSvc.GetVariables(scriptProvider.RootSuite[0][2][1][6], ref list);
            Assert.AreEqual(4, list.Count);
            Assert.AreEqual("COMMAND_PATTERN=%m %p", list[0]);
            Assert.AreEqual(@"TEST_RUNNER=c:\Fitnesse\dotnet\FitServer.exe", list[1]);
            Assert.AreEqual(@"path=c:\Fitnesse\dotnet\fit.dll", list[2]);
            Assert.AreEqual("path=eg.dll", list[3]);
        }

        [TestMethod]
        public void TestGetAsmPaths()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection02\FitnesseRoot\GrandSuite1\Suite1\TestCase2");
            Assert.IsNotNull(scriptProvider, "script provider is null");

            scriptProvider.LoadScripts();

            var utilSvc = _testServiceContext.Services.Get<IFitUtilService>();
            IList<string> list = new List<string>();
            utilSvc.GetVariables(scriptProvider.RootSuite[0][2][1][6], ref list);
            string[] asms = utilSvc.GetAsmPaths(list);
            Assert.AreEqual(2, asms.Length);
            Assert.AreEqual(@"c:\Fitnesse\dotnet\fit.dll", asms[0]);
            Assert.AreEqual("eg.dll", asms[1]);
        }


        [TestMethod]
        public void TestGetAllChildren()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection02\FitnesseRoot\GrandSuite1\Suite1\TestCase2");
            Assert.IsNotNull(scriptProvider, "script provider is null");

            scriptProvider.LoadScripts();
            var utilSvc = _testServiceContext.Services.Get<IFitUtilService>();
            IList<ITestNode> list = new List<ITestNode>();

            ITreeChildEnumerator treeEnumerator = scriptProvider.RootSuite[0][2][1][6].GetTreeChildEnumerator();
            foreach (ITestNode item in treeEnumerator)
            {
                list.Add(item);
            }
            Assert.AreEqual(11, list.Count);
        }

        [TestMethod]
        public void TestLocalFitScriptProviderBug1()
        {
            var scriptProvider = _testServiceContext.Services.Get<IFitLocalTestScriptProvider>("testsession",
                                                                                               @"TestDataCollection03\FitnesseRoot\GrandSuite1");
            Assert.IsNotNull(scriptProvider, "script provider is null");
            scriptProvider.LoadScripts();
            Assert.AreEqual(1, scriptProvider.RootSuite.Count);
            Assert.AreEqual(3, scriptProvider.RootSuite[0].Count);
            Assert.AreEqual(7, scriptProvider.RootSuite[0][1].Count);

            Assert.IsNotNull(scriptProvider.RootSuite[0][1] as ISuite);
            Assert.IsNotNull(scriptProvider.RootSuite[0][2] as ISuite);
            Assert.IsNotNull(scriptProvider.RootSuite[0][2][2] as ITestCase);

            ITestNode aTestCase = scriptProvider.RootSuite[0][2][1][6];
            Assert.AreEqual(4, aTestCase.Count);
            string txt = (aTestCase as IFitRunnableDoc).GetTestDocument();
            Assert.AreEqual("TestCase11", aTestCase.Name);
        }
    }
}